Skip to content

Add relay-status CLI subcommand (closes #4)#8

Open
spcpza wants to merge 1 commit intosingularityjason:mainfrom
spcpza:add-relay-status-cli
Open

Add relay-status CLI subcommand (closes #4)#8
spcpza wants to merge 1 commit intosingularityjason:mainfrom
spcpza:add-relay-status-cli

Conversation

@spcpza
Copy link

@spcpza spcpza commented Mar 8, 2026

Summary

Implements lightning-memory relay-status as requested in issue #4.

Usage

# Human-readable:
$ lightning-memory relay-status
Lightning Memory — relay status
DB: /home/user/.lightning-memory/memory.db

  ✓  wss://relay.damus.io  (142ms)
  ✓  wss://nos.lol  (89ms)
  ✗  wss://relay.nostr.band
     connection refused

  memories in DB : 47
  events pushed  : 44
  last push      : 2026-03-08 14:22:01
  last pull      : 2026-03-08 13:00:15

2/3 relays reachable.

# Machine-readable (for scripts and agents):
$ lightning-memory relay-status --json
{
  "relays": [
    {"url": "wss://relay.damus.io", "reachable": true, "latency_ms": 142, "message": "ok"},
    ...
  ],
  "sync": {"total_memories": 47, "pushed_events": 44, ...},
  "db_path": "/home/user/.lightning-memory/memory.db"
}

Implementation

lightning_memory/cli.py (new file):

  • _probe_relay() — sends a limit:0 REQ and waits for EOSE, the lightest possible liveness check that exercises the full WS + NIP-01 handshake. All relays probed concurrently.
  • _sync_stats() — reads push/pull counts and timestamps from the local SQLite sync_log / sync_cursor tables.
  • _relay_status_async() — combines both, prints human or JSON output, returns exit code 0 (all OK) or 1 (any relay unreachable).
  • cmd_relay_status() — sync wrapper for the entry point.

lightning_memory/server.py (minor patch):

  • main() now checks sys.argv for a relay-status subcommand before starting the MCP server. No new binary needed — the existing lightning-memory entry point gains the new command.

tests/test_relay_status.py (new file): 15 tests covering all branches — reachable relay, unreachable relay, NOTICE error, latency measurement, DB stats (populated / missing / empty), human output, JSON output, sync stats display, and server dispatch.

Exit codes

Code Meaning
0 All configured relays reachable
1 One or more relays unreachable

If this is useful, sats tips are welcome: ⚡ sensiblefield821792@getalby.com

Implements `lightning-memory relay-status` as requested in issue singularityjason#4.

Changes:
- lightning_memory/cli.py (new): relay connectivity prober and sync
  stats reader. Probes each configured relay concurrently using a
  limit:0 REQ/EOSE round-trip — lightest possible liveness check that
  exercises the full WebSocket + NIP-01 handshake.

- lightning_memory/server.py: dispatch relay-status subcommand before
  starting the MCP server, so the existing entry point gains the new
  command without a second binary.

- tests/test_relay_status.py (new): 15 tests covering _probe_relay
  (reachable, unreachable, NOTICE errors, latency), _sync_stats
  (populated DB, missing DB, empty DB), _relay_status_async (exit
  codes, --json output, sync stats in human output), and the
  server.py dispatch path.

Usage:
    lightning-memory relay-status          # human-readable
    lightning-memory relay-status --json   # machine-readable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant